package com.suixun.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suixun.dao.monitor.MonitorManageDAO;
import com.suixun.dao.studentmanage.StudentManageDAO;
import com.suixun.entity.MapRegion;
import com.suixun.entity.MonitorInfo;
import com.suixun.entity.StdObjectinfo;
import com.suixun.entity.page.PageModel;
import com.suixun.service.MonitorService;
import com.suixun.util.DateUtil;
import com.suixun.util.StringUtil;
import com.suixun.vo.MonitorVO;
import com.suixun.vo.SafeRegionVO;
import com.suixun.vo.StudentVO;
/**
 * 
 * <p>Title: MonitorServiceImpl</p>
 * <p> 功能描述:实时监控Service </p> 
 * @author	陈显强
 * @date	2017年1月11日下午12:25:50
 * @version 1.0
 */
@Service("monitorService")
public class MonitorServiceImpl implements MonitorService {

	@Autowired
	private MonitorManageDAO monitorManageDAO;
	@Autowired
	private StudentManageDAO studentManageDAO;
	
	
	/**
	 * <p>Title: queryMapMonitorList</p>
	 * <p> 功能描述:查询实时地图监控数据 </p>
	 * @param monitor
	 * @return
	 */
	@Override
	public List<MonitorVO> selectMapMonitorList(MonitorVO monitor) {
		PageModel<MonitorVO> pageModel = new PageModel<MonitorVO>();
		if(monitor == null) {
			List<MonitorInfo> monitorList = monitorManageDAO.queryMapMonitorList();
			return monitorInfoToVO(monitorList,monitor);
		}
		if(monitor != null && monitor.getObjectidList() != null) {
			pageModel.getQueryparam().put("objectids",monitor.getObjectidList());
		}
		List<MonitorInfo> monitorList = monitorManageDAO.queryMonitorsList(pageModel);
		return monitorInfoToVO2(monitor,monitorList);
	}

	
	/**
	 * <p>Title: queryMonitorList</p>
	 * <p> 功能描述:分页查询实时监控列表 </p>
	 * @param monitor
	 * @return
	 */
	@Override
	public PageModel<MonitorVO> selectMonitorList(MonitorVO monitor) {
		PageModel<MonitorVO> pageModel = new PageModel<MonitorVO>();
			pageModel.getQueryparam().put("objectids",monitor.getObjectidList());
			List<MonitorInfo> monitorList = monitorManageDAO.queryMonitorsList(pageModel);
			pageModel.setQueryresult(monitorInfoToVO2(monitor,monitorList));
			pageModel.setCurrentpage(monitor.getCurrentpage());
			pageModel.setTotlepage(monitor.getTotlepage());
			pageModel.setTotlerows(monitor.getTotlerows());
			return pageModel;
		}
	
	public List<MonitorVO> monitorInfoToVO2(MonitorVO monitor,List<MonitorInfo> monitorList) {
		List<MonitorVO> monitorVOs = new LinkedList<>();
		for(int i = 0;i < monitor.getObjectList().size();i++) {
			StudentVO student = monitor.getObjectList().get(i);
			MonitorVO monitorVO = new MonitorVO();
			int flag = 0;
			for(int j = 0;j < monitorList.size();j++) {
				if(Integer.parseInt(student.getObjectid()) == monitorList.get(j).getObjectid()) {
					if (monitorList.get(j).getLon() == 0 && monitorList.get(j).getLat() == 0 && 
							monitorList.get(j).getLonx() == 0 && monitorList.get(j).getLaty() == 0) {
						continue;
					}
					flag = 1;
					monitorVO.setObjectid(student.getObjectid());
					monitorVO.setMdtid(student.getMdtid());
					monitorVO.setSimid(student.getSimid());
					if(monitorList.get(j).getOnlinestatus() == 0) {
						monitorVO.setStatus("0");
					}else {
						monitorVO.setStatus("1");
					}
					monitorVO.setAlarmtype(String.valueOf(monitorList.get(j).getAlarmtype()));
					monitorVO.setAlarmvalue(String.valueOf(monitorList.get(j).getAlarmvalue()));
					monitorVO.setMaxrecvtime(DateUtil.formatDateToString(monitorList.get(j).getMaxrecvtime(), null));
					monitorVO.setLon(String.valueOf(monitorList.get(j).getLon()));
					monitorVO.setLat(String.valueOf(monitorList.get(j).getLat()));
					monitorVO.setLonx(String.valueOf(monitorList.get(j).getLonx()));
					monitorVO.setLaty(String.valueOf(monitorList.get(j).getLaty()));
					monitorVOs.add(monitorVO);
				}
			}
			if(flag == 0) {
				monitorVO.setObjectid(student.getObjectid());
				monitorVO.setMdtid(student.getMdtid());
				monitorVO.setSimid(student.getSimid());
				monitorVO.setStatus("1");
				monitorVOs.add(monitorVO);
			}
			monitorVO = null;
		}
		return monitorVOs;
	}
	
	public List<MonitorVO> monitorInfoToVO(List<MonitorInfo> monitorList,MonitorVO monitor) {
		List<MonitorVO> monitorVOs = new LinkedList<>();
		for(MonitorInfo monitorInfo:monitorList) {
			if (monitorInfo.getLon() == 0 && monitorInfo.getLat() == 0 && 
					monitorInfo.getLonx() == 0 && monitorInfo.getLaty() ==0) {
				continue;
			}
			MonitorVO monitorVO = new MonitorVO();
			monitorVO.setObjectid(String.valueOf(monitorInfo.getObjectid()));
			if(monitor == null) {
				monitorVO.setStatus("0");
			}else {
				if(monitorInfo.getOnlinestatus() == 0) {
					monitorVO.setStatus("0");
				}else {
					monitorVO.setStatus("1");
				}
			}
			monitorVO.setAlarmtype(String.valueOf(monitorInfo.getAlarmtype()));
			monitorVO.setAlarmvalue(String.valueOf(monitorInfo.getAlarmvalue()));
			if(monitor != null && monitor.getObjectid() != null) {
				monitorVO.setRecvtime(DateUtil.formatDateToString(monitorInfo.getRecvtime(), null));
			}else {
				monitorVO.setMaxrecvtime(DateUtil.formatDateToString(monitorInfo.getMaxrecvtime(), null));
			}
			monitorVO.setLon(String.valueOf(monitorInfo.getLon()));
			monitorVO.setLat(String.valueOf(monitorInfo.getLat()));
			monitorVO.setLonx(String.valueOf(monitorInfo.getLonx()));
			monitorVO.setLaty(String.valueOf(monitorInfo.getLaty()));
			monitorVOs.add(monitorVO);
			monitorVO = null;
		}
		return monitorVOs;
	} 

	/**
	 * <p>Title: follow</p>
	 * <p> 功能描述:获取跟踪设备的历史轨迹 </p>
	 * @param monitor
	 * @return
	 */
	@Override
	public List<MonitorVO> selectMonitorFollow(MonitorVO monitor) {
		Map<String,Object> map = new HashMap<>();
		map.put("objectid", monitor.getObjectid());
		List<MonitorInfo> monitorList = monitorManageDAO.queryMapMonitorsByFollow(map);
		if(monitorList.size() == 0) {
			return null;
		}
		return monitorInfoToVO(monitorList,monitor);
	}
	
	
	/**
	 * <p>Title: querySafeRegionList</p>
	 * <p> 功能描述:查询安全区域列表 </p>
	 * @param monitor
	 * @return
	 */
	@Override
	public PageModel<SafeRegionVO> querySafeRegionList(MonitorVO monitor) {
		PageModel<SafeRegionVO> pageModel = new PageModel<SafeRegionVO>();
		String mdtid = null,simid = null,schoolName = null;
		Integer totleRows = 0;
		List<SafeRegionVO> regionVOs = new LinkedList<>();
		if(StringUtil.isNotEmpty(monitor.getMdtid())) {
			mdtid = monitor.getMdtid();
		}
		if(StringUtil.isNotEmpty(monitor.getSimid())) {
			simid = monitor.getSimid();
		}
		if(StringUtil.isNotEmpty(monitor.getSchoolname())) {
			schoolName = monitor.getSchoolname();
			pageModel.getQueryparam().put("schoolname", schoolName);
			totleRows = monitorManageDAO.countSafeRows(pageModel);
			pageModel.setTotlepage(totleRows);
			if(monitor != null) {
				if(StringUtil.isNotEmpty(monitor.getPagenumber())) {
					pageModel.setCurrentpage(Integer.parseInt(monitor.getPagenumber()));
				}
			}
			List<MapRegion> mapRegionList = monitorManageDAO.querysafeRegionList(pageModel);
			if(mapRegionList.size() > 0) {
				pageModel.setQueryresult(mapRegionToSafeRegionVO(mapRegionList, regionVOs));
			}else {
				pageModel.setQueryresult(null);
			}
			return pageModel;
		}
		pageModel.getQueryparam().put("mdtid", mdtid);
		pageModel.getQueryparam().put("simid", simid);
		List<StdObjectinfo> studentList = studentManageDAO.queryStduentList(pageModel);
		if(studentList.size() > 0) {
			String[] regionIds = studentList.get(0).getTrackregionInList().split(",");
			if(regionIds.length > 0) {
				List<Integer> regionids = new LinkedList<>();
				for(String regionid:regionIds) {
					if(StringUtil.isNotEmpty(regionid)) {
						regionids.add(Integer.parseInt(regionid));
					}
				}
				pageModel.getQueryparam().put("regionids", regionids);
				totleRows=monitorManageDAO.countSafeRows(pageModel);
				pageModel.setTotlepage(totleRows);
				if(monitor != null) {
					if(StringUtil.isNotEmpty(monitor.getPagenumber())) {
						pageModel.setCurrentpage(Integer.parseInt(monitor.getPagenumber()));
					}
				}
				List<MapRegion> mapRegionList = monitorManageDAO.querysafeRegionList(pageModel);
				pageModel.setQueryresult(mapRegionToSafeRegionVO(mapRegionList, regionVOs));
			}else {
				pageModel.setQueryresult(null);
			}
			return pageModel;
		}
		pageModel.setQueryresult(null);
		return pageModel;
	}


	public List<SafeRegionVO> mapRegionToSafeRegionVO(List<MapRegion> regionList,List<SafeRegionVO> regionVOs){
		for(MapRegion mapRegion:regionList) {
			SafeRegionVO regionVO = new SafeRegionVO();
			regionVO.setRegionid(String.valueOf(mapRegion.getRegionid()));
			regionVO.setCreatetime(mapRegion.getCreatetime() == null ? null : DateUtil.formatDateToString(mapRegion.getCreatetime(), null));
			regionVO.setCreateuser(mapRegion.getUsername() == null ? null : mapRegion.getUsername());
			regionVO.setRadius(mapRegion.getRadius()==null ? null : String.valueOf(mapRegion.getRadius()));
			regionVO.setRegionaddr(mapRegion.getRegioninfo() == null ? null : mapRegion.getRegioninfo());
			regionVO.setRegionname(mapRegion.getRegionname() == null ? null : mapRegion.getRegionname());
			regionVO.setCenterlat(mapRegion.getCenterlat() == null ? null : String.valueOf(mapRegion.getCenterlat()));
			regionVO.setCenterlon(mapRegion.getCenterlon() == null ? null : String.valueOf(mapRegion.getCenterlon()));
			regionVO.setMaxlat(mapRegion.getMaxlat() == null ? null : String.valueOf(mapRegion.getMaxlat()));
			regionVO.setMaxlon(mapRegion.getMaxlon() == null ? null : String.valueOf(mapRegion.getMaxlon()));
			regionVO.setMinlat(mapRegion.getMinlat() == null ? null : String.valueOf(mapRegion.getMinlat()));
			regionVO.setMinlon(mapRegion.getMinlon() == null ? null : String.valueOf(mapRegion.getMinlon()));
			regionVO.setRegiontype(mapRegion.getRegiontype() == null ? null:String.valueOf(mapRegion.getRegiontype()));
			regionVOs.add(regionVO);
			regionVO = null;
		}
		return regionVOs;
	}
	
	/**
	 * <p>Title: addSafeRegion</p>
	 * <p> 功能描述:添加安全区域 </p>
	 * @param region
	 * @return
	 * @throws Exception 
	 */
	@Override
	@Transactional
	public int addSafeRegion(SafeRegionVO region) {
		MapRegion mapRegion = new MapRegion();
		mapRegion.setRegionname(region.getRegionname());
		mapRegion.setRegioninfo(region.getRegionaddr());
		mapRegion.setRadius(Integer.parseInt(region.getRadius()));
		mapRegion.setCreateuser(Integer.parseInt(region.getCreateuser()));
		mapRegion.setRegiontype(Integer.parseInt(region.getRegiontype()));
		switch(region.getRegiontype()) {
			case "0":
				if(!StringUtil.isNotEmpty(region.getMinlon()) || !StringUtil.isNotEmpty(region.getMinlat()) || 
						!StringUtil.isNotEmpty(region.getMaxlon()) || !StringUtil.isNotEmpty(region.getMaxlat())) {
					return -1;
				}
				mapRegion.setMinlon(Integer.parseInt(region.getMinlon()));
				mapRegion.setMinlat(Integer.parseInt(region.getMinlat()));
				mapRegion.setMaxlon(Integer.parseInt(region.getMaxlon()));
				mapRegion.setMaxlat(Integer.parseInt(region.getMaxlat()));
				break;
			case "1":
				if(!StringUtil.isNotEmpty(region.getCenterlon()) || !StringUtil.isNotEmpty(region.getCenterlat())) {
					return -1;
				}
				mapRegion.setCenterlon(Integer.parseInt(region.getCenterlon()));
				mapRegion.setCenterlat(Integer.parseInt(region.getCenterlat()));
				break;
		}
		Date nowDate = new Date();
		mapRegion.setCreatetime(nowDate);
		monitorManageDAO.addSafeRegion(mapRegion);
		List<String> mdtids = new LinkedList<>();
		Map<String,Object> map = new HashMap<>();
		if("1".equals(region.getType())) {
			String[] mdtidArr = region.getMdtids().split(",");
			for(String mdtid:mdtidArr) {
				mdtids.add(mdtid);
			}
		}else {
			if("3".equals(region.getType())) {
				map.put("simid",region.getSimid());
			}else {
				mdtids.add(region.getMdtids());
			}
		}
		map.put("regionid", mapRegion.getRegionid());
		if(mdtids.size() == 0){
			studentManageDAO.updateStudent(map);
		}else {
			for(String mdtid:mdtids) {
				map.put("mdtid", mdtid);
				studentManageDAO.updateStudent(map);
			}
		}
		
		return 0;
	}
	

	/**
	 * <p>Title: updateSafeRegion</p>
	 * <p> 功能描述:修改安全区域</p>
	 * @param region
	 * @return
	 */
	@Override
	@Transactional
	public int updateSafeRegion(SafeRegionVO region) {
		MapRegion mapRegion = new MapRegion();
		mapRegion.setRegionname(region.getRegionname());
		mapRegion.setRegioninfo(region.getRegionaddr());
		mapRegion.setRadius(region.getRadius() == null ? null : Integer.parseInt(region.getRadius()));
		mapRegion.setCreateuser(region.getCreateuser() == null ? null : Integer.parseInt(region.getCreateuser()));
		mapRegion.setMinlon(region.getMinlon() == null ? null : Integer.parseInt(region.getMinlon()));
		mapRegion.setMinlat(region.getMinlat() == null ? null : Integer.parseInt(region.getMinlat()));
		mapRegion.setMaxlon(region.getMaxlon() == null ? null : Integer.parseInt(region.getMaxlon()));
		mapRegion.setMaxlat(region.getMaxlat() == null ? null : Integer.parseInt(region.getMaxlat()));
		mapRegion.setCenterlon(region.getCenterlon() == null ? null : Integer.parseInt(region.getCenterlon()));
		mapRegion.setCenterlat(region.getCenterlat() == null ? null : Integer.parseInt(region.getCenterlat()));
		mapRegion.setRegiontype(region.getRegiontype() == null ? null : Integer.parseInt(region.getRegiontype()));
		Date nowDate = new Date();
		mapRegion.setUpdatetime(nowDate);
		mapRegion.setRegionid(Integer.parseInt(region.getRegionid()));
		int isUpdate = monitorManageDAO.updateSafeRegion(mapRegion);
		return isUpdate;
	}

	/**
	 * <p>Title: deleteSafeRegion</p>
	 * <p> 功能描述:删除安全区域 </p>
	 * @param region
	 * @return
	 */
	@Override
	@Transactional
	public int deleteSafeRegion(SafeRegionVO region) {
		List<StdObjectinfo> StdObjectinfoList = studentManageDAO.queryByRegionId(Integer.parseInt(region.getRegionid()));
		for(int i = 0;i < StdObjectinfoList.size();i++) {
			String[] trackregionInList = StdObjectinfoList.get(i).getTrackregionInList().split(new StringBuffer().append(",").append(region.getRegionid()).toString());
			StringBuffer trackregionIns = new StringBuffer();
			for(String trackregionIn:trackregionInList) {
				trackregionIns.append(trackregionIn);
			}
			Map<String,Object> map = new HashMap<>();
			map.put("objectid", StdObjectinfoList.get(i).getObjectid());
			map.put("trackregion_in_list", trackregionIns.toString());
			studentManageDAO.updateTrackregionList(map);
		}
		return monitorManageDAO.deleteSafeRegion(Integer.parseInt(region.getRegionid()));
	}
	
	
}
